<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 16/8/27
 * Time: 19:30
 */

namespace app\services\controller;


use app\common\controller\LiteBase;
use app\common\util\FileUploader;
use app\common\util\OSSFactory;
use OSS\OssClient;
use think\Config;
use think\Exception;

class Uploader extends LiteBase
{
    public function uploadImg()
    {
        return $this->_upload('image');
    }

    public function uploadFile()
    {
        return $this->_upload('file');
    }

    public function uploadMedia()
    {
        return $this->_upload('media');
    }

    public function uploadFlash()
    {
        return $this->_upload('flash');
    }

    private function _upload($fileType)
    {
        $_type = I('get._t', '', 'trim');
        $uploader = new FileUploader(['fileType' => $fileType]);
        $msg = $uploader->saveFile();
        if (!$msg) {
            if ($_type == 'editor') {
                $data = [
                    "originalName" => $uploader->getOriName(),
                    "name" => $uploader->getFileName(),
                    "url" => ($uploader->getUploadFileUrl()),
                    "size" => $uploader->getFileSize(),
                    "type" => $uploader->getFileExt(),
                    "error" => 0,
                    "state" => 'SUCCESS'
                ];
                return $this->returnJson($data);
            }
            return $this->jsonSuccess(($uploader->getUploadFileUrl()));
        }
        if ($_type == 'editor') {
            return $this->returnJson([
                'error' => 1,
                'message' => $msg
            ]);
        }
        return $this->jsonFail($msg);
    }
}